package com.nisure.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nisure.entities.FormCustomerEntity;
import com.nisure.service.FormCustomerService;

@RestController
public class FormCustomerController {
	@Autowired
	private FormCustomerService formCustomerService;

	@RequestMapping("/maxid")
	public Map<String, Object> findMaxId() {
		Integer maxId_one = formCustomerService.findMaxId("db1");
		Integer maxId_two = formCustomerService.findMaxId("db2");
		List<FormCustomerEntity> list = new ArrayList<>();
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> params = new HashMap<>();
		if (maxId_one > maxId_two) {
			params.put("id", maxId_two);
			list = formCustomerService.selectNeedHandleData(params, "db1");
		}
		if (!list.isEmpty()) {
			formCustomerService.insertFormCustomers(list, "db2");
		}
		map.put("code", 0);
		map.put("message", "success");

		return map;
	}

	@RequestMapping("/getNeedData")
	public Map<String, Object> getNeedData() {
		Map<String, Object> map = new HashMap<>();
		List<FormCustomerEntity> list = formCustomerService.selectNeedData(new HashMap<>(), "db5");
		for(int i=0; i<list.size();i++){
			System.out.println("正在处理第"+(i+1)+"条数据");
			FormCustomerEntity fce = list.get(i);
			FormCustomerEntity fff = new FormCustomerEntity();
			String companyName = fce.getCompanyName();
			String city = fce.getCity();
			String url = fce.getUrl();
			String phone = fce.getPhone();
			String remark = fce.getRemark();
			int randid = fce.getRandid();
			fff.setCompanyName(companyName);
			fff.setUrl(url);
			fff.setCity(city);
			StringBuffer sb = new StringBuffer();
			if(null != phone && phone.length()>=11){
				if(null != getTelnum(phone) && getTelnum(phone).length()>0){
//					fff.setPhone(getTelnum(phone));
					sb.append(getTelnum(phone));
				}
			}
			if(null != remark && remark.length()>=11){
				if(null != getTelnum(remark) && getTelnum(remark).length()>0){
//					fff.setRemark(getTelnum(remark));
					sb.append(",");
					sb.append(getTelnum(remark));
				}
			}
			if(sb.length() >= 11){
				fff.setPhone(sb.toString());
			}
			fff.setRandid(randid);
			formCustomerService.insertFormCustomer(fff, "db4");
		}
		
		map.put("code", 0);
		map.put("message", "success");
		return map;
	}

	public static String getTelnum(String sParam) {
		if (sParam.length() <= 0 || null == sParam)
			return "";
		Pattern pattern = Pattern.compile("((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$*");
		Matcher matcher = pattern.matcher(sParam);
		StringBuffer bf = new StringBuffer();
		while (matcher.find()) {
			bf.append(matcher.group()).append(",");
		}
		int len = bf.length();
		if (len > 0) {
			bf.deleteCharAt(len - 1);
		}
		return bf.toString();
	}
	public static void main(String[] args) {
		System.out.println(getTelnum("·刘经理4000441668刘经理18922290710丁启军15992436646刘英18922290710彭先生18769502386"));
	}
	
	@RequestMapping("/getNeedHandleData")
	public Map<String, Object> getNeedHandleData() {
		Map<String, Object> map = new HashMap<>();
		map.put("queryDiscoverCount", 1);
		map.put("totalData", 0);
		List<FormCustomerEntity> list = formCustomerService.selectNeedDataList(map, "db1");
		formCustomerService.getNeedHandleData(map, "db1");
		map.put("dataList", list);
		return map;
	}
}
